/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.dispositivos;

import com.mapping.Dispositivo;
import com.mapping.Software;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "altaSoftware", results = {@Result(name = "success", location = "/GestionDispositivos/rAltaSoftware.jsp"),
                                              @Result(name = "error",location="/GestionDispositivos/eAltaSoftware.jsp")})
public class AltaSoftwareAction extends ActionSupport{
    private String idDisp;
    private String idSoft;
    private Software software;
    private String btnEliminar;
    @Override
    public String execute(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Dispositivo disp = (Dispositivo)em.getReference(Dispositivo.class,idDisp);
        software = (Software)em.getReference(Software.class,Integer.parseInt(idSoft));
        software.getDispositivoCollection().add(disp);
        disp.getSoftwareCollection().add(software);
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setIdDisp(String idDisp) {
        this.idDisp = idDisp;
    }

    public void setIdSoft(String idSoft) {
        this.idSoft = idSoft;
    }

    public Software getSoftware() {
        return software;
    }

    public String getBtnEliminar() {
        return btnEliminar;
    }

    public void setBtnEliminar(String btnEliminar) {
        this.btnEliminar = btnEliminar;
    }
    
}
